package com.yima.baseproj.module.app.dict.data;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yima.baseproj.module.app.dict.data.model.AppDictDataAddParamModel;
import com.yima.baseproj.module.app.dict.data.model.AppDictDataEditParamModel;
import com.yima.baseproj.module.app.dict.data.model.AppDictDataSearchModel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class AppDictDataService extends ServiceImpl<AppDictDataMapper, AppDictDataEntity> {

    public Page<Map> findByPage(Page page, AppDictDataSearchModel searchVo) {
        return baseMapper.findBySearchVo(page,searchVo);
    }

    public List<Map> findByList(AppDictDataSearchModel searchVo) {
        return baseMapper.findBySearchVo(searchVo);
    }

    /**
     * 添加 - 事务
     * @param paramModel
     */
    @Transactional(rollbackFor = Exception.class)
    public void add(AppDictDataAddParamModel paramModel) {

    }

    @Transactional(rollbackFor = Exception.class)
    public void edit(AppDictDataEditParamModel paramModel) {

    }

    @Transactional(rollbackFor = Exception.class)
    public void del(String id) {
        removeById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void dels(String[] ids) {
        for (String id : ids) {
            removeById(id);
        }
    }

    public AppDictDataEntity findByDictIdAndType(String dictId, String type) {
        return getOne(Wrappers.<AppDictDataEntity>query().eq("dict_id",dictId).eq("type",type));
    }

    public List<AppDictDataEntity> findAllByDictId(String dictId) {
        return list(Wrappers.<AppDictDataEntity>query().eq("dict_id",dictId).orderByAsc("sort"));
    }

    public long countAllByTypeAndDataType(String type, String dataType) {
        return baseMapper.countAllByTypeAndDataType(type,dataType);
    }
}